﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Core.Authenticated;
using Core.Library;
using DataObject;
using DataObject.Category;

namespace CMS.Controls.Category
{
    public partial class CategoryManager : AuthenticatedControl
    {
        protected int Product;
        protected int Article;
        protected int PageIndex = SettingValues.PageIndex;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                switch (ConvertUtility.ToInt32(cControl.Params))
                {
                    case (int)CategoryType.Product:
                        ltTitle.Text = "Danh mục sản phẩm";
                        break;
                    case (int)CategoryType.Article:
                        ltTitle.Text = "Chuyên mục tin tức";
                        break;
                }

                GetStatistics();

                ListCategory();
            }
        }

        #region Statistics

        private void GetStatistics()
        {
            Product = CategoryController.Category_GetStatistic((int)CategoryType.Product);
            Article = CategoryController.Category_GetStatistic((int)CategoryType.Article);
        }

        #endregion

        private void ListCategory()
        {
            var categories = new List<CategoryObject>();
            CategoryController.GetListTree(categories, ConvertUtility.ToInt32(cControl.Params), 0, 0, 1); // -1 bao gồm: Active và Not Active
            lvCategory.DataSource = categories;
            lvCategory.DataBind();
        }

        protected void lvCategory_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                if (String.Equals(e.CommandName, "DeleteCategory"))
                {
                    var dataItem = (ListViewDataItem)e.Item;
                    var id = ConvertUtility.ToInt32(lvCategory.DataKeys[dataItem.DisplayIndex].Value);

                    if (id > 0)
                    {
                        CategoryController.Category_Delete(id);

                        ltMessage.Text = MessageUtility.ShowMessage(string.Empty, "Xóa dữ liệu thành công.", MessageType.Success);

                        ListCategory();
                    }
                }
            }
            catch
            {
                ltMessage.Text = MessageUtility.ShowMessage(string.Empty, 
                                    "Bạn cần xóa các thông tin liên quan đến sản phẩm. Trước khi xóa danh mục.", MessageType.Information);
            }
        }

        protected void lvCategory_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var ltUrl       = (Literal)e.Item.FindControl("ltUrl");

                var categoryId  = ((HiddenField)e.Item.FindControl("hfCategoryId")).Value;
                var name        = ((HiddenField)e.Item.FindControl("hfName")).Value;
                var parentId    = ConvertUtility.ToInt32(((HiddenField) e.Item.FindControl("hfParentId")).Value);                

                ltUrl.Text = string.Format(parentId == 0 
                                ? "<a href=\"{0}\"><strong class=\"green\">{1}</strong></a>" 
                                : "<a href=\"{0}\">{1}</a>", UrlHelpers.EditCategory(cControl.Params, categoryId), name);
            }
        }

        protected void BtnCreate(object sender, EventArgs e)
        {
            Response.Redirect(UrlHelpers.CreateCategory(ConvertUtility.ToInt32(cControl.Params)));    
        }
    }
}